#include"com.h"

void Usage(string proc)
{
  cout<<"Usage: "<<proc<<" id"<<" port"<<endl;
}

int main(int argc,char* argv[])
{
  if(argc!=3)
  {
    Usage(argv[0]);
    return 1;
  }
  string svr_ip= argv[1];
  uint16_t port = atoi(argv[2]);

  int sock = socket(AF_INET,SOCK_STREAM,0);
  if(sock<0)
  {
    cerr<<"socket fail"<<endl;
    return 2;
  }

  struct sockaddr_in peer;
  peer.sin_family=AF_INET;
  peer.sin_port = htons(port);
  // 1.将点分十进制的字符串转换成4字节ip，2. 将四字节有主机序列转网络序列
  peer.sin_addr.s_addr = inet_addr(svr_ip.c_str());

    if(connect(sock,(struct sockaddr*)&peer,sizeof(peer))<0)
    {
      cout<<"connet fail"<<endl;
      return 3;
    }
    cout<<"connect success!"<<endl;   
    while(1)
    {
      cout<<"please Enter#: ";
      char buffer[1024]={0};
      fgets(buffer,sizeof(buffer)-1,stdin);
      
      write(sock,buffer,strlen(buffer));
     // memset(buffer,'\0',sizeof(buffer));
      ssize_t s = read(sock,buffer,sizeof(buffer));
      if(s>0)
      {
        buffer[s]=0;
      cout<<"server echo#: "<<buffer<<endl;
      }

    }


  return 0;
}
